﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Koonsoft.HYCRM.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Koonsoft.HYCRM.DAL
{
    /// <summary>
    /// Summary description for ObjectCacheDAL
    /// </summary>
    public class ObjectCacheDAL
    {
        private static Database database = DatabaseFactory.CreateDatabase("KOONCRMDB");

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="objectId"></param>
        /// <returns></returns>
        public static ObjectCache GetObjectCache(string objectId)
        {
            const string sql = @"select * from ObjectCache where objectid=@objectid";

            using(var cmd = database.GetSqlStringCommand(sql))
            {
                database.AddInParameter(cmd, "@objectid", DbType.String,objectId);

                var rd = (RefCountingDataReader)database.ExecuteReader(cmd);
                var sqlrd = (SqlDataReader)rd.InnerReader;

                if (sqlrd.HasRows)
                {
                    sqlrd.Read();
                    return new ObjectCache
                    {
                        ObjectData = sqlrd["ObjectData"].ToString(),
                        CacheDate = Convert.ToDateTime(sqlrd["cachedate"]),
                        ObjectID = sqlrd["ObjectID"].ToString(),
                        OperateType = Convert.ToInt32(sqlrd["OperateType"])
                    };
                }
            }

            return null;
        }

        /// <summary>
        /// 新增缓存对象
        /// </summary>
        /// <param name="objectCache"></param>
        /// <returns></returns>
        public static bool AddObjectCache(ObjectCache objectCache)
        {
            const string procedure = @"Cache_AddObject";

            using (var cmd = database.GetStoredProcCommand(procedure))
            {
                database.AddInParameter(cmd, "@objectid", DbType.String, objectCache.ObjectID);
                database.AddInParameter(cmd, "@objectdata", DbType.Xml, objectCache.ObjectData);
                database.AddInParameter(cmd, "@operatetype", DbType.Int32, objectCache.OperateType);

                return database.ExecuteNonQuery(cmd) == 1;
            }
        }

        /// <summary>
        /// 更新缓存对象
        /// </summary>
        /// <param name="objectCache"></param>
        /// <returns></returns>
        public static bool UpdateObjectCache(ObjectCache objectCache)
        {
            const string sql = @"update objectcache set ObjectData=@objectData,CacheDate=GETDATE(),OperateType=1 where ObjectID=@objectID";

            using (var cmd = database.GetSqlStringCommand(sql))
            {
                database.AddInParameter(cmd, "@objectid", DbType.String, objectCache.ObjectID);
                database.AddInParameter(cmd, "@objectdata", DbType.Xml, objectCache.ObjectData);

                return database.ExecuteNonQuery(cmd) == 1;
            }
        }
    }

}
